<?php
function get_items_cate_list($id=0,$level=0){
	$items_cate_mod=D('items_cate');
	$list=array();
	$join="LEFT JOIN ".C('DB_PREFIX')."collect_taobao as ct ON ct.cate_id=".C('DB_PREFIX')."items_cate.id";

	$res=$items_cate_mod->cache(true)->order('ordid DESC')->where('pid='.$id)
	->join($join)
	->select();
	foreach($res as $key=>$val){
		$val['level']=$level;
		$list[$val['id']]=$val;
		//二级分类
		$arr=$items_cate_mod->cache(true)
		->order('ordid DESC')
		->where('pid='.$val['id'])->join($join)
		->select();
		//三级分类
		foreach($arr as $k2=>$v2){
			$v2['level']=$level+1;
			$v2['cls']="sub_".$val['id'];
			$list[$v2['id']]=$v2;

			$res3=$arr[$k2]['items']=$items_cate_mod->cache(true)
			->order('ordid DESC')
			->where('pid='.$v2['id'])
			->join($join)->select();
			foreach($res3 as $k3=>$v3){
				$v3['level']=$level+2;
				$v3['cls']="sub_".$val['id']." sub_".$v2['id'];
				$list[$v3['id']]=$v3;
			}
		}
		$res[$key]['items']=$arr;
	}
	return array('list'=>$res,'sort_list'=>$list);
}

//根据Url和页数获取网页内容
function ReturnPingContent($url, $pageIndex)
{
	$url = $url + $pageIndex;
	$contents = file_get_contents($url);
	//如果出现中文乱码使用下面代码
	$getcontent = iconv("gb2312", "utf-8//ignore",$contents);
	return $getcontent;
}

//根据Url获取网页内容
function  ReturnResponse($url)
{
	$contents = file_get_contents($url);
	//如果出现中文乱码使用下面代码
	$getcontent = iconv("gb2312", "utf-8//ignore",$contents);
	return $getcontent;
}


//根据商品详细网页内容获得淘宝店商品评论总数
function GetTaobaoMerchandiseCommentCount($MerchandiseDetail)
{
	$CommentCountContent;
	//根据商品详细界面获取商品评论总数网页内容
	if(preg_match('/data-commonApi[\\w\\W]*?\">/',$MerchandiseDetail,$CommentCounturl))
	{
		if(count($CommentCounturl)>0)
		{
			preg_match('/http[\\w\\W]*?\"/',$CommentCounturl[0],$CommentCounturl);

			if(count($CommentCounturl)>0)
			{
				$CommentCounturl=trim(str_replace('\\','',$CommentCounturl[0]));
				$CommentCountContent =ReturnResponse($CommentCounturl);
			}
		}
	}

	$CommentCount=0;
	//获取评价总数
	if(preg_match('/total[\\w\\W]*?}/',$CommentCountContent,$CommentCountMaths))
	{
		if(count($CommentCountMaths)>0)
		{
			preg_match("/[0-9]+/",$CommentCountMaths[0],$CommentCountMaths);
			if(count($CommentCountMaths)>0)
			{
				$CommentCount+=$CommentCountMaths[0];
			}
		}
	}
	return $CommentCount;
}


//根据商品详细网页内容获得商品评论总数
function GetMerchandiseCommentCount($MerchandiseDetail)
{
	$CommentCountContent;
	//根据商品详细界面获取商品评论总数网页内容
	if(preg_match('/apiTmallReviewCount[\\w\\W]*?\",/',$MerchandiseDetail,$CommentCounturl))
	{
		if(count($CommentCounturl)>0)
		{
			preg_match('/http[\\w\\W]*?\"/',$CommentCounturl[0],$CommentCounturl);

			if(count($CommentCounturl)>0)
			{
				$CommentCounturl=trim(str_replace('\\','',$CommentCounturl[0]));
				$CommentCountContent =ReturnResponse($CommentCounturl);
			}
		}
	}

	$CommentCount=0;
	//获取评价总数
	if(preg_match('/rateTotal[\\w\\W]*?,/',$CommentCountContent,$CommentCountMaths))
	{
		if(count($CommentCountMaths)>0)
		{
			preg_match("/[0-9]+/",$CommentCountMaths[0],$CommentCountMaths);
			if(count($CommentCountMaths)>0)
			{
				$CommentCount+=$CommentCountMaths[0];
			}
		}
	}
	return $CommentCount;


}
//根据商品详细网页内容获得商品评价
function GetMerchandiseComments($MerchandiseDetail)
{
	$MerchandiseComments=array();
	$Nick;
	$rateContent;
	$rateDate;
	$UserNumId;

	$CommentContent;
	//获取评价详情地址
	if(preg_match('/valReviewsApi[\\w\\W]*?\",/',$MerchandiseDetail,$Commenturl))
	{
		if(count($Commenturl)>0)
		{
			preg_match('/http[\\w\\W]*?\"/',$Commenturl[0],$Commenturl);
			if(count($Commenturl)>0)
			{
				$Commenturl=trim(str_replace('\\','',$Commenturl[0]));
			}
		}

	}

	for($page=1;$page<=1;$page++)
	{
	
		$strreplace="currentPage=".$page;
		$Commenturl=str_replace('currentPage=1', $strreplace, $Commenturl);
	
		$CommentContent = ReturnResponse($Commenturl);
		//获取评价详情
		if(preg_match_all('/aliMallSeller[\\w\\W]*?},/',$CommentContent,$CommentMaths))
		{
			if(count($CommentMaths)>0)
			{
				for ($i = 0; $i <count($CommentMaths[0]); $i++)
				{
					$CommentMath=$CommentMaths[0][$i];
					if(preg_match('/displayUserNick\":\"[\\w\\W]*?\",/',$CommentMath,$Nick))
					{
						if(count($Nick)>0)
						{
							$Nick=explode(':',$Nick[0]);
							if(count($Nick)>0)
							{
								$Nick=trim(trim($Nick[1],',\"'));
							}
						}
					}
					if(preg_match('/rateContent\":\"[\\w\\W]*?\",/',$CommentMath,$rateContent))
					{
						if(count($rateContent)>0)
						{
							$rateContent=explode(':',$rateContent[0]);
							if(count($rateContent)>0)
							{
								$rateContent=trim(trim($rateContent[1],',\"'));
							}
						}
					}
					if(preg_match('/rateDate[\\w\\W]*?\",/',$CommentMath,$rateDate))
					{
						if(count($rateDate)>0)
						{
							$rateDate=preg_split('/:\"/',$rateDate[0]);
							if(count($rateDate)>0)
							{
								$rateDate= trim($rateDate[1],',\"');
							}
						}
					}

					if(preg_match('/displayUserNumId\":[\\w\\W]*?,/',$CommentMath,$UserNumId))
					{
						if(count($UserNumId)>0)
						{
							$UserNumId=explode(':',$UserNumId[0]);
							if(count($UserNumId)>0)
							{
								$UserNumId=trim(trim($UserNumId[1],',\"'));
							}
						}
					}
					$comment= array();
					$comment['Nick']=$Nick;
					$comment['Content']=$rateContent;
					$comment['dtCommentDateTime']=strtotime(str_replace('.', '-', $rateDate));
					$comment['UserNumId']=$UserNumId;
					$exists=false;
					for ($j = 0; $j <count($MerchandiseComments); $j++)
					{
						if($MerchandiseComments[$j]['UserNumId']==$UserNumId)
						{
							$exists=true;
							break;
						}
						else
						{
							$exists=false;
						}
					}
				
					if($exists==false)
					{
						$MerchandiseComments[Count($MerchandiseComments)]=$comment;
					}
				}
			}
		}
	}
	return $MerchandiseComments;
}


